import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJSX from '@vitejs/plugin-vue-jsx'
import banner from 'vite-plugin-banner'
import cssnano from 'cssnano'
import visualizer from 'rollup-plugin-visualizer';
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
import {author, license, name, version} from './package.json'

const extendsPlugins = [];

function getBanner(banner, pkg) {
    if (!banner || typeof banner === 'string') {
        return banner || '';
    }
    banner = {...pkg, ...(banner === true ? {} : banner)};
    const author = banner.author
    const license = banner.license || '';
    return (
        '/*!\n' +
        ' * arco-design-designer 可视化表单设计器\n' +
        ` * ${banner.name} v${banner.version}\n` +
        ` * (c) ${author || ''}\n` +
        (license && ` * Released under the ${license} License.\n`) +
        ' */'
    );
}

const __banner__ = {
    author: `2024-${new Date().getFullYear()} ${author}\n * Gitee https://gitee.com/dotor-ww/arco-design-designer\n * Site https://form-create.com/`,
    license,
    name,
    version
}

// 打包生产环境才引入的插件
if (process.env.NODE_ENV === 'production') {
    // 打包依赖展示
    extendsPlugins.push(
        visualizer({
            open: false,
            gzipSize: false,
            brotliSize: false,
        })
    );
}

export default defineConfig({
    build: {
        lib: {
            entry: 'src/index.js',
            name: 'FcDesigner',
            fileName: format => `index.${format}.js`,
        },
        rollupOptions: {
            output: {
                exports: 'named',
                globals: {
                    vue: 'Vue'
                }
            },
            external: [
                'vue',
                '@arco-design/web-vue',
                '@form-create/arco-design'
            ],

        },
        brotliSize: true
    },
    css: {
        postcss: {
            plugins: [
                cssnano({
                    preset: 'advanced'
                })
            ]
        }
    },
    plugins: [vue(), vueJSX(), banner(getBanner(__banner__)), cssInjectedByJsPlugin(), ...extendsPlugins]
})
